﻿import request from '/@/utils/request';
enum Api {
  AddIotsdk = '/api/iotsdk/add',
  DeleteIotsdk = '/api/iotsdk/delete',
  UpdateIotsdk = '/api/iotsdk/update',
  PageIotsdk = '/api/iotsdk/page',
  DetailIotsdk = '/api/iotsdk/detail',
  UploadSdkName = '/api/iotsdk/UploadSdkName',
  UploadSdkFileUrl = '/api/iotsdk/UploadSdkFileUrl',
  GetDatabaseInfo = '/api/iotsdk/GetDatabaseInfo',
}

// 增加SDK管理
export const addIotsdk = (params?: any) =>
	request({
		url: Api.AddIotsdk,
		method: 'post',
		data: params,
	});

// 删除SDK管理
export const deleteIotsdk = (params?: any) => 
	request({
			url: Api.DeleteIotsdk,
			method: 'post',
			data: params,
		});

// 编辑SDK管理
export const updateIotsdk = (params?: any) => 
	request({
			url: Api.UpdateIotsdk,
			method: 'post',
			data: params,
		});

// 分页查询SDK管理
export const pageIotsdk = (params?: any) => 
	request({
			url: Api.PageIotsdk,
			method: 'post',
			data: params,
		});

// 详情SDK管理
export const detailIotsdk = (id: any) => 
	request({
			url: Api.DetailIotsdk,
			method: 'get',
			data: { id },
		});

		export const getDatabaseInfo = () => 
			request({
					url: Api.GetDatabaseInfo,
					method: 'get'
				});

	/**
	* 上传SDK名称 
	*/
export const uploadSdkName = (params: any) =>
		uploadFileHandle(params, Api.UploadSdkName)
	/**
	* 上传下载地址 
	*/
export const uploadSdkFileUrl = (params: any) =>
		uploadFileHandle(params, Api.UploadSdkFileUrl)

	export const uploadFileHandle = (params: any, url: string) => { 
	const formData = new window.FormData();
	formData.append('file', params.file);
	//自定义参数
	if (params.data) {
		Object.keys(params.data).forEach((key) => {
			const value = params.data![key];
			if (Array.isArray(value)) {
				value.forEach((item) => {
					formData.append(`${key}[]`, item);
				});
				return;
			}
			formData.append(key, params.data![key]);
		});
	}
	return request({
		url: url,
		method: 'POST',
		data: formData,
		headers: {
			'Content-type': 'multipart/form-data;charset=UTF-8',
			// ts-ignore
			ignoreCancelToken: true,
		},
	});
};
